package com.terios.logic.ui.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.terios.Comment;
import com.terios.R;
import com.terios.ReplyComment;
import com.terios.logic.ui.imaCache.Anseylodar;
import com.terios.logic.ui.util.CommentViewHolder;

/**
 * CommentAdapter.java 2012-10-20 下午1:13:25
 * 
 * @author Wang Tengrui
 */
public class CommentAdapter extends BaseAdapter {
	public List<Comment> comments;// 评论
	Context mContext;// 上下文
	Anseylodar anseylodar;
	Date nowDate;
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");// 格式化日期

	public CommentAdapter(Context context, List<Comment> allComments) {
		comments = allComments;
		mContext = context;
		anseylodar = new Anseylodar();
		nowDate = new Date();
	}

	@Override
	public int getCount() {
		// 由于我们最后一项时更多 所以这里getcount+1
		return comments.size();
	}

	@Override
	public Object getItem(int position) {
		return comments.get(position);
	}

	// 请求更多的数据
	public void addmoreDate(List<Comment> addmore) {
		if (comments != null) {
			this.comments.addAll(addmore);// 把新传得数据加到现在的list中
			this.notifyDataSetChanged();// 将数据追加到ListView中显示
		}
	}

	@Override
	public long getItemId(int position) {
		return comments.get(position).getId();
	}

	@SuppressWarnings("static-access")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View commentView = null;
		if (null != convertView) {
			commentView = convertView;
		}else {
			commentView = LayoutInflater.from(mContext).inflate(R.layout.comment_item_view, null);
		}
		CommentViewHolder commentViewHolder = new CommentViewHolder();
		
		//查找评论UI
		commentViewHolder.msgCommentIvItemPortrait = (ImageView)commentView.findViewById(R.id.msgCommentIvItemPortrait);//头像
		commentViewHolder.msgCommentIvItemV = (ImageView)commentView.findViewById(R.id.msgCommentIvItemV);//新浪认证图标
		commentViewHolder.msgCommentTvItemName = (TextView)commentView.findViewById(R.id.msgCommentTvItemName);//用户名
		commentViewHolder.msgCommentTvItemDate = (TextView)commentView.findViewById(R.id.msgCommentTvItemDate);//评论发表时间
		commentViewHolder.msgCommentTvItemContent = (TextView)commentView.findViewById(R.id.msgCommentTvItemContent);//评论内容
		commentViewHolder.msgCommentTvItemSubContent = (TextView)commentView.findViewById(R.id.msgCommentTvItemSubContent);//评论的微博或评论
		
		//填充数据
		Comment mComment = comments.get(position);
		if (mComment.getUser().isVerified()) {
			commentViewHolder.msgCommentIvItemV.setVisibility(View.VISIBLE);// 显示认证图标
		}else {
			commentViewHolder.msgCommentIvItemV.setVisibility(View.GONE);// 隐藏认证图标
		}
		// 填充发布该评论的用户名
		commentViewHolder.msgCommentTvItemName.setText(mComment.getUser().getScreenName());
		long timeOffset = (nowDate.getTime() - mComment.getCreatedAt().getTime()) / 60000;// 与当前时间相差多少
		// 填充该评论的发布时间
		if (timeOffset < 60) {
			commentViewHolder.msgCommentTvItemDate.setText(timeOffset + "分钟前");
		} else {
			commentViewHolder.msgCommentTvItemDate.setText(dateFormat.format(mComment.getCreatedAt()).toString());
		}
		String usericon = mComment.getUser().getProfileImageUrl();
		commentViewHolder.msgCommentIvItemPortrait.setImageResource(R.drawable.portrait);// 重置头像
		anseylodar.showimgAnsy(commentViewHolder.msgCommentIvItemPortrait, usericon);
		
		commentViewHolder.msgCommentTvItemContent.setText(mComment.getText());//设置评论内容
		ReplyComment mReplyComment = mComment.getReply_comment();
		if (null != mReplyComment) {
			commentViewHolder.msgCommentTvItemSubContent.setText("回复 我 的评论：" + mReplyComment.getText());
		}else {
			commentViewHolder.msgCommentTvItemSubContent.setText("评论 我 的微博：" + mComment.getStatus().getText());
		}
		return commentView;
	}

}
